﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManagePageVideos : Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_PAGEVIDEO = "__PAGEVIDEO::PAGEVIDEO";
    private const string PROP_DATASRC_Pages = "__PAGES::PAGES";
    private const string PROP_LAYOUT = "__PAGEVIDEO::UIL";
    private const string PROP_SELECTED_PAGEVIDEO_ID = "__PAGEVIDEO::SPAGEVIDEOID";

    #endregion

    #region Public Properties

    public PageVideoCollection DataSourcePageVideo
    {
        get { return (SessionMgr.Session[PROP_DATASRC_PAGEVIDEO] is PageVideoCollection ? (PageVideoCollection)SessionMgr.Session[PROP_DATASRC_PAGEVIDEO] : (PageVideoCollection)(SessionMgr.Session[PROP_DATASRC_PAGEVIDEO] = new PageVideoCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_PAGEVIDEO] = value; }
    }

    public int SelectedPageVideoID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_PAGEVIDEO_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_PAGEVIDEO_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_PAGEVIDEO_ID] = value; }
    }


    public PageContentsCollection DataSourcePages
    {
        get { return (SessionMgr.Session[PROP_DATASRC_Pages] is PageContentsCollection ? (PageContentsCollection)SessionMgr.Session[PROP_DATASRC_Pages] : (PageContentsCollection)(SessionMgr.Session[PROP_DATASRC_Pages] = new PageContentsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_Pages] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }




    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourcePageVideo = PageVideo.GetAllPageVideos();
            VideoList1.DataBind(DataSourcePageVideo);
        }
    }

    #endregion

    #region Control: VideoList

    protected void ucVideoList_EditRequested(object sender, Controls_VideoList.EditRequestedEventArgs evt)
    {
        SelectedPageVideoID = evt.PV.VideoID;
        //VideoViewEdit1.DataBind(evt.PV);
        SetLayout(LayoutType.Edit);
    }

    protected void ucVideoList_AddNewRequested(object sender, EventArgs evt)
    {
        VideoViewEdit1.CancelEdit();
        PageVideo newPageVideo = PageVideo.CreateNew(DataSourcePageVideo);
        SelectedPageVideoID = newPageVideo.VideoID;
        DataSourcePages = PageContents.GetAllPageContents();
        VideoViewEdit1.DataBind(DataSourcePageVideo.FindByVideoID(SelectedPageVideoID),DataSourcePages);
        SetLayout(LayoutType.Edit);
    }


    protected void ucVideoList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourcePageVideo = PageVideo.GetAllPageVideos();
        VideoList1.DataSource = DataSourcePageVideo;
    }

    #endregion

    #region Control: VideoViewEdit

    protected void ucVideoViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        VideoViewEdit1.DataSourcePageVideo = DataSourcePageVideo.FindByVideoID(SelectedPageVideoID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        VideoViewEdit1.CancelEdit();
        // If we were adding a new video...then remove the incomplete/not added new video object from the collection.
        if (DataSourcePageVideo.FindByVideoID(SelectedPageVideoID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourcePageVideo.Remove(DataSourcePageVideo.FindByVideoID(SelectedPageVideoID));
                SelectedPageVideoID = (DataSourcePageVideo[0] != null ? DataSourcePageVideo[0].VideoID : 0);
            }
            catch
            {
            }
        }
        VideoList1.DataBind(DataSourcePageVideo);
        SetLayout(LayoutType.View);
    }

    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if(VideoViewEdit1.EndEdit())
        {
            PageVideo obj = DataSourcePageVideo.FindByVideoID(SelectedPageVideoID);
            PageVideo.Update(obj);

            // TODO: Set Feedback text...

            VideoList1.DataBind(DataSourcePageVideo);
            SetLayout(LayoutType.View);

        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlPageVideosList.Visible = false;
                pnlPageVideosViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:

            case LayoutType.Search:

                pnlPageVideosList.Visible = true;
                pnlPageVideosViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


}
